function [ F, ks,ps , badpoints,corrs, FFormatted, corrsFormatted,EFormatted,spacePts,width,height,FFormattedCLEAN,FCLEAN ] = generateF3D( fdiff, skew, ar,centerdeviation,silent,number,noiselevel,numCorruptFs,width,height ,gaussianerrorstd,differentframes )

if(width==0 || height==0)
    width=1024;
    height=768;
end

writefiles=1;
numCorrs=200;
FFormatted=cell(number,number);
EFormatted=cell(number,number);
corrsFormatted=cell(number,number);
badpoints=zeros(numCorrs,((number*(number-1))/2) );
if (nargin <11)
    gaussianerrorstd =0;
end
if (nargin <12)
    differentframes=0;
end

[ ps ,myks,ts,rs ] = generatePs3D( fdiff, skew, ar,centerdeviation, number,1,silent,0 ,width,height,differentframes);


if(writefiles==1)
    dirname=['projFolder' num2str(sum(round(100*clock)))];
    mkdir(dirname);
    for i=1:number
        K=myks{1,i};
        save([dirname '/K_' num2str(i) '.txt'], 'K','-ascii', '-double');
    end
    fid = fopen([dirname '/F_index.txt'], 'w');
end



if(noiselevel>1)
    disp('noise level is hjigher than 1');
    noiselevel=1;
end
if(numCorruptFs>((number*(number-1))/2))
    disp('demanded more corrupt Fs than there are Fs');
end




sizepts=200;
x1=zeros(3, numCorrs,number);
x1c=zeros(3, numCorrs,number);
X=zeros(4, numCorrs);
for q=1:numCorrs
    tryq=0;
    flagbad=1;
    
    while(flagbad==1)
        flagbad=0;
        if(tryq>100)
            error('too many rejections');
        end
        
        X(1,q)=rand()*sizepts -(sizepts/2);
        X(2,q)=rand()*sizepts -(sizepts/2);
        X(3,q)=rand()*sizepts -(sizepts/2);
        X(4,q)=1;
        
        for i=1:number
            x1(:,q,i)=ps{1,i}*X(:,q);
            x1(:,q,i)=x1(:,q,i)/x1(3,q,i);
            
            X1Gaussian= randn()*gaussianerrorstd ;
            Y1Gaussian= randn()*gaussianerrorstd  ;
            
            x1c(:,q,i)= x1(:,q,i);
            x1(1,q,i)= x1(1,q,i)+  X1Gaussian;
            x1(2,q,i)= x1(2,q,i)+  Y1Gaussian;
            
            if(outofbnd(x1(:,q,i),width,height)==1)
                tryq=tryq+1;
                flagbad=1;
                break;
            end
        end
        
    end
    
    
end

count=0;
for i=1:number
    for j=1:i
        if(i~=j)
            count=count+1;
            xlC=x1c(:,:,i);
            xrC=x1c(:,:,j);
            
            xl=x1(:,:,i);
            xr=x1(:,:,j);
            
            FFormattedCLEAN{i,j}=fundmatrix( xlC,xrC);
            FFormattedCLEAN{j,i}=fundmatrix( xlC,xrC)';
            [FCLEAN{count}, e1, e2] = fundmatrix( xlC,xrC);
            
            if(count<=numCorruptFs)
                for q=1:ceil(numCorrs*noiselevel)
                    if(rand()<0.5)
                        xl(1,q)=rand()*width;
                        xl(2,q)=rand()*height;
                        
                    else
                        xr(1,q)=noisex*width;
                        xr(2,q)=noisey*height;
                    end
                end
            end
            
            
            
            corrs{1,count}=xl;
            corrs{2,count}=xr;
            
            corrsFormatted{i,j}=[xl ; xr]  ;
            corrsFormatted{j,i}=[xr ; xl]  ;
            
            [F{count}, e1, e2] = fundmatrix(xl,  xr);
            %     display([' F (' num2str(count) ')  is between frames ' num2str(i) ' and ' num2str(j) ]);
            FFormatted{i,j}=F{count};
            FFormatted{j,i}=F{count}';
            
            EFormatted{i,j}=(myks{j}')*FFormatted{i,j}*(myks{i});
            EFormatted{j,i}=(myks{i}')*FFormatted{j,i}*(myks{j});
            
            
            
            
        end
    end
end




if(writefiles==1)
    figure
    hold
    for k=1:number
        plot3(ts{1,k}(1,1),ts{1,k}(2,1),ts{1,k}(3,1), 'marker','o','markersize',5,'markeredgecolor',[0,1,0],'markerfacecolor',[0,1,0]);
        
    end
    
    for k=1:numCorrs
        plot3(X(1,k),X(2,k),X(3,k), 'marker','o','markersize',5,'markeredgecolor',[1,0,0],'markerfacecolor',[1,0,0]);
        
    end

    v=ones(500,500,3);
    for k=1:number
        figure
   title('matches')
         plot(x1(1,:,k),x1(2,:,k),'r+');
         xlim([0 width]);
          ylim([0 height]);
    end
    
   
    
% count=0;
% for i=1:number
%     for j=1:i
%         if(i~=j)
%             count=count+1;
%               
%             m1=x1(:,:,i);
%             m2=x1(:,:,j);
%             if(size(corrs{1,countF},2)>8)
%                 [bd] = funddist(F{count}, [xl ;  xr]);
%             end
%             fprintf(fidstat,[ num2str(countF) ' , '  num2str(i)  ' , '  num2str(j)  ' , '  num2str(in)  ' , '  num2str(size(xl,  xr,2)) ' , ' num2str(mean(bd)) ' , ' num2str(median(bd)) ' , ' num2str(max(bd)) ' \n ']);
%             
%             figure('Visible','off'); show(im1,0), title('Putative matches'), hold on
%             for n = 1:length(m1);
%                 line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)])
%             end
%             saveas(gcf,imagedirty);
%             
%             % Display both images overlayed with inlying matched feature points
%             figure('Visible','off');  show(double(im1)+double(im2),0), title('Inlying matches'), hold on
%             plot(m1(2,inliers),m1(1,inliers),'r+');
%             plot(m2(2,inliers),m2(1,inliers),'g+');
%             
%             for n = inliers
%                 line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)],'color',[0 0 1])
%             end
%             saveas(gcf, imageclean);
%             
%         end
%     end
% end

end


ks=myks;
spacePts=X;



end

function [b]=outofbnd(x,width,height)
b=0;
if(x(1,1)> width || x(1,1)<0)
    b=1;
end

if(x(2,1)> height || x(2,1)<0)
    b=1;
end


end